		
		<h1>Send E-mail</h1>
		
		<form id="emailform" action="" method="post" autocomplete="off" data-confirm-send="false">
			<?php $this->load->helper('string'); ?>
			<input type="hidden" id="token" name="token" value="<?=random_string('unique')?>" />
			<table class="greentable">
			<tr>
				<td>To&nbsp;</td>
				<td>
						<select id="to" name="to">
								<option value="all">All users</option>
								<option value="specific"<?=@$default_rcpt?' selected':''?>>Specific recepients</option>
						</select>
						&nbsp;<span style="display:none;" id="rcpt-select"><a class="lightbox" data-fancybox-type="iframe" href="javascript:void(0);">select users</a></span> <!--<br /><a href="javascript:void(0);" onclick="jQuery('#row-bcc').toggle('slow');">add Bcc</a> 
						  <a href="javascript:void(0);" onclick="jQuery('#row-cc').toggle('slow');">add Cc</a>-->
				&nbsp;</td>
			</tr>
			<tr id="row-rcpt-list_display" style="display:none;">
				<td>Recepients&nbsp;</td>
				<td>
					<span id="rcpt-list_display"></span>
				</td>
			</tr>
			<tr id="row-bcc" style="display:none;">
				<td>Bcc&nbsp;</td>
				<td><input style="width:275px;" type="text" id="bcc" name="bcc" value="">&nbsp;</td>
			</tr>
			<tr id="row-cc" style="display:none;">
				<td>Cc&nbsp;</td>
				<td><input style="width:275px;" type="text" id="cc" name="cc" value="">&nbsp;</td>
			</tr>
			<tr>
				<td>Subject&nbsp;</td>
				<td><input style="width:500px;" type="text" id="subject" name="subject" value="">&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>
					<textarea rows="10" cols="20" id="message" name="message" class="tinymce"></textarea>&nbsp;
					<div id="mergefields" style="display:none;border:1px solid #8FBCE9;background:#ffffff;color:#000000;padding:5px;height:200px;overflow:auto;font-size:10px;z-index:10;">
							
							<ul>
									<li><a href="#" onclick="insertMergeField('{client_id}');return false">ID</a>: <a href="#" onclick="insertMergeField('{client_id}');return false">{client_id}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_name}');return false">Client Name</a>: <a href="#" onclick="insertMergeField('{client_name}');return false">{client_name}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_first_name}');return false">First Name</a>: <a href="#" onclick="insertMergeField('{client_first_name}');return false">{client_first_name}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_last_name}');return false">Last Name</a>: <a href="#" onclick="insertMergeField('{client_last_name}');return false">{client_last_name}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_country}');return false">Country</a>: <a href="#" onclick="insertMergeField('{client_country}');return false">{client_country}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_email}');return false">E-mail Address</a>: <a href="#" onclick="insertMergeField('{client_email}');return false">{client_email}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_password}');return false">Password</a>: <a href="#" onclick="insertMergeField('{client_password}');return false">{client_password}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_phonenumber}');return false">Phone Number</a>: <a href="#" onclick="insertMergeField('{client_phonenumber}');return false">{client_phonenumber}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_address1}');return false">Address 1</a>: <a href="#" onclick="insertMergeField('{client_id}');return false">{client_address1}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_address2}');return false">Address 2</a>: <a href="#" onclick="insertMergeField('{client_address2}');return false">{client_address2}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_city}');return false">City</a>: <a href="#" onclick="insertMergeField('{client_city}');return false">{client_city}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_state}');return false">State/Region</a>: <a href="#" onclick="insertMergeField('{client_state}');return false">{client_state}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_company}');return false">Company</a>: <a href="#" onclick="insertMergeField('{client_company}');return false">{client_company}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_postcode}');return false">Postcode</a>: <a href="#" onclick="insertMergeField('{client_postcode}');return false">{client_postcode}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_mt4_id}');return false">MT4 ID</a>: <a href="#" onclick="insertMergeField('{client_mt4_id}');return false">{client_mt4_id}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_mirror_login_id}');return false">Mirror login ID</a>: <a href="#" onclick="insertMergeField('{client_mirror_login_id}');return false">{client_mirror_login_id}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_group}');return false">MT4 Group</a>: <a href="#" onclick="insertMergeField('{client_group}');return false">{client_group}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_mt4_agent}');return false">MT4 Agent</a>: <a href="#" onclick="insertMergeField('{client_mt4_agent}');return false">{client_mt4_agent}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_leverage}');return false">Leverage</a>: <a href="#" onclick="insertMergeField('{client_leverage}');return false">{client_leverage}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_stored_doc}');return false">Stored doc</a>: <a href="#" onclick="insertMergeField('{client_stored_doc}');return false">{client_stored_doc}</a></li>
									<li><a href="#" onclick="insertMergeField('{client_signature_doc}');return false">Signature image</a>: <a href="#" onclick="insertMergeField('{client_signature_doc}');return false">{client_signature_doc}</a></li>
							</ul>
					</div>
					<a href="javascript:void(0);" onclick="$('#mergefields').toggle('slow');">Show/Hide merge fields</a>
					
				</td>
			</tr>
			<tr>
				<td>Attach a file&nbsp;</td>
				<td>						
						<iframe style="display:none;" id="email_file-control" height="50px" frameborder="0" src="<?=$base_url?>admin/upload_email_file/" /></iframe> 
						<span id="email_file-control_stat"></span>
						<span id="email_file-list"></span>
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>
					<input type="submit" id="btSend" name="btSend" value="Send Now!">&nbsp;
				</td>
			</tr>
			</table>
		</form>
		<div id="dialog"></div>
		<script language="javascript">		
			var recepients 	   = [];
			var uploadingFile  = false;
			var sendEmailStart = false;
			
			jQuery(document).ready(function(){
					jQuery('#emailform').submit(function(){
							if(uploadingFile) 
							{
									jQuery('#btSend').after('<span id="wait-flash" style="color:#ff0000;">&nbsp;&nbsp;Please wait until file upload is complete...</span>');
									setTimeout('$("#wait-flash").remove();', 3000);
									sendEmailStart = true;
							}
							else
							{
									sendEmail();
							}
							return false;
					});
					jQuery('#emailform #to').change(function(){
							if(jQuery(this).val()=='specific') jQuery('#rcpt-select').show();
							else 							   jQuery('#rcpt-select').hide();
					});
					$('#cancel-upload').click(function(){
							
					});
					
					
					// $('#subject,#message').focus(function(){
							// alert($(this).attr('id') + ' has focus');
					// });
			});
			
			jQuery('#email_file-control').ready(function(){
					uploadingFile = false;
					jQuery('#email_file-control').show('slow');
			});
			
			function insertMergeField(str){
					if(str) {
							// alert(str);
							tinyMCE.execInstanceCommand('message',"mceInsertContent",false,str);
					}
			}
			
			function sendEmail() {
					var rcpt_list = '';
					if(recepients.length>0)
					{
						$.each(recepients, function(i, obj){
							if(typeof obj.mt4_uid!='undefined') 
							{
								rcpt_list = rcpt_list + (rcpt_list ? '|' : '') + obj.mt4_uid;
							} 
						});
					}
					jQuery.blockUI({ message: '<h3><img src="'+__base_url+'images/busy.gif" /> Sending...</h3>' });
					var postdata = 'token=' + $('#token').val();
					postdata 	 = postdata + '&to='		+ encodeURIComponent(jQuery.trim($('#to').val()));
					postdata 	 = postdata + '&rcpt_list='	+ encodeURIComponent(jQuery.trim(rcpt_list));
					postdata 	 = postdata + '&subject='	+ encodeURIComponent(jQuery.trim($('#subject').val()));
					postdata 	 = postdata + '&message='	+ encodeURIComponent(jQuery.trim($('#message').tinymce().getContent()));
					jQuery.ajax({
							type: 'POST',
							url: __base_url+'admin/jx_send_email',
							data: postdata,
							cache:false,
							success: function(msg){
									jQuery.unblockUI();
									// alert(msg);
									try {
											// var _obj = jQuery.parseJSON(msg);
											alert('E-mail successfully sent!');	
									} catch(err) {
											
									}
							},
							error: function(xhr,ajaxOptions,thrownError){
									jQuery.unblockUI();
									alert(xhr.responseText);
							}
					});
			}
			
			function cancelUpload(){
					jQuery('#email_file-control').attr('src', __base_url+'admin/upload_email_file/');
					jQuery('#email_file-control_stat').html('<strong style="color:#ff0000;">Upload aborted</strong>');
					setTimeout("jQuery('#email_file-control_stat').html('');jQuery('#email_file-control').show('slow');", 2000);
			}
			
			function didFileSelected(file_name) {
					if(file_name=='') return false;
					// alert('didFileSelected -> ['+file_name+']');
					try {
							jQuery("#email_file-control").contents().find("#token").val($('#token').val());
							jQuery('#email_file-control').contents().find("#upload_form").submit();
							jQuery('#email_file-control').hide();
							jQuery('#email_file-control_stat').html('<strong style="color:orange;">Uploading file...</strong> (<a href="javascript:void(0);" id="cancel-upload" onclick="cancelUpload();">cancel</a>)');
							uploadingFile = true;
					} catch(err){}
			}
			
			function didUploadError(json) {
					uploadingFile = false;
					jQuery('#email_file-control').show();
					jQuery('#email_file-control_stat').html('');
					try {
							var _obj = $.parseJSON(json);
							if(_obj.error) 	jQuery('#email_file-control_stat').html('<br /><strong style="color:#ff0000;">'+_obj.error+'</strong>');
					} catch(err){ }
			}
			
			function didUploadDone(json) {
					uploadingFile = false;
					// alert('didUploadDone -> ['+json+']');
					jQuery.ajax({
							type: 'POST',
							url: __base_url+'admin/jx_get_email_files',
							data: 'token='+$('#token').val(),
							cache:false,
							success: function(msg){
									jQuery('#email_file-control').attr('src', __base_url+'admin/upload_email_file/');
									setTimeout("jQuery('#email_file-control_stat').html('');jQuery('#email_file-control').show('slow');", 2000);
									// alert(msg);
									if(sendEmailStart) 
									{
											sendEmailStart = false;
											sendEmail();
									}
									try {
											var _obj = $.parseJSON(msg);
											if(typeof _obj.items!='undefined')
											{
													$('#email_file-list').html(_obj.items);
											}
									} catch(err){ }
							},
							error: function(xhr,ajaxOptions,thrownError){
									alert(xhr.responseText);
							}
					});
			}
			
			function jx_delete_email_file(token, file_name) {
					if(!confirm('Are you sure that you want to delete file ['+file_name+'] ?')) return false;
					jQuery.ajax({
							type: 'POST',
							url: __base_url+'admin/jx_delete_email_file',
							data: 'token='+token+'&file_name='+encodeURIComponent(jQuery.trim(file_name)),
							cache:false,
							success: function(msg){
									// alert(msg);
									jQuery.ajax({
											type: 'POST',
											url: __base_url+'admin/jx_get_email_files',
											data: 'token='+$('#token').val(),
											cache:false,
											success: function(msg){
													// alert(msg);
													try {
															var _obj = $.parseJSON(msg);
															if(typeof _obj.items!='undefined')
															{
																	$('#email_file-list').html(_obj.items);
															}
													} catch(err){ }
											},
											error: function(xhr,ajaxOptions,thrownError){
													alert(xhr.responseText);
											}
									});
							},
							error: function(xhr,ajaxOptions,thrownError){
									alert(xhr.responseText);
							}
					});
			} 
				
			function userSelected(str) {
					if(str)
					{
						var p = str.split('|');
						if(p.length==4)
						{
								if(!rcptExists(p[1])) 
								{
										var obj = {
											id: 		p[0], 
											mt4_uid: 	p[1], 
											firstname: 	p[2], 
											lastname: 	p[3]
										};
										recepients.push(obj);
										displayRcptList();
								}
						}
					}
			}
			
			function displayRcptList() {
					if(recepients.length>0)
					{
						var str = '';
						$.each(recepients, function(i, obj){
							if(typeof obj.mt4_uid!='undefined') 
							{
								str = str + (str ? ', ' : '') + '<a href="javascript:void(0);" onclick="removeRcpt(\''+obj.mt4_uid+'\');">' + obj.firstname + (obj.firstname!='' && obj.lastname!=''?' ':'') + obj.lastname + ' (#'+obj.mt4_uid+')' + '</a>';
							} 
						});
						// alert(str);
						$('#rcpt-list_display').html(str);
						$('#row-rcpt-list_display').show();
					}
					else
					{
						$('#rcpt-list_display').html('');
						$('#row-rcpt-list_display').hide();
					}
			}
			
			function rcptExists(mt4_uid) {
					var ret = false;
					$.each(recepients, function(i, obj){
							if(typeof obj.mt4_uid!='undefined' && mt4_uid==obj.mt4_uid) 
							{
								ret  = true;
								return true;
							}
					});
					return ret;
			}
			
			function removeRcpt(mt4_uid) {
					// alert('remove: #'+mt4_uid);
					recepients = jQuery.grep(recepients, function(obj) {
							return obj.mt4_uid != mt4_uid;
					});
					displayRcptList();
			}
			
		</script>
		
		